package tools;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.sql.PreparedStatement;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Tester02 {
	
	public static double[] dx(double[] input, int w, int h, double[] output) {
		for (int i = 0; i<input.length; i++) {
			int x = i%w, y = i/w;
			int index = (x+w+1)%w+y*w;
			output[i] = -(input[i]-input[index]);
		}
		return output;
	}
	
	public static double[] dy(double[] input, int w, int h, double[] output) {
		for (int i = 0; i<input.length; i++) {
			int x = i%w, y = i/w;
			int index = x+((y+1+h)%h)*w;
			output[i] = input[i]-input[index];
		}
		return output;
	}
	
	public static void main(String[] args) {
		final int w = 9, h = 9, wh = w/2, hh = h/2;
		double sigma = (w/2.)/3.;
		
		final double[] kernel = new double[w*h];
		
		for (int y = 0; y<h; y++) {
			for (int x = 0; x<w; x++) {
				
				kernel[x+y*w] = Math.exp(-((x-wh)*(x-wh)+(y-hh)*(y-hh))/(2*sigma*sigma)); 
			}
		}
		
		double[] dx = dx(kernel, w, h, new double[w*h]);
		double[] dxx = dx(dx, w, h, new double[w*h]);
		double[] dxy = dy(dx, w, h, new double[w*h]);
		
		double[] dy = dy(kernel, w, h, new double[w*h]);
		double[] dyy = dy(dy, w, h, new double[w*h]);
		double[] dyx = dx(dy, w, h, new double[w*h]);
		
		int[] lum = Tester01.prepare(Image.haargausxx, new int[w*h]);
		
		int[] pixels = new int[w*h];
		
 		final BufferedImage show = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
 		final BufferedImage show2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		
		pixels = Tester01.fuseRGB(lum, lum, lum, pixels);
		show.setRGB(0, 0, w, h, pixels, 0, w);

		int[] lum2 = Tester01.prepare(dxy, new int[w*h]);
		pixels = Tester01.fuseRGB(lum2, lum2, lum2, pixels);
		show2.setRGB(0, 0, w, h, pixels, 0, w);
		try {
			final BufferedImage lena = ImageIO.read(Tester02.class.getClassLoader().getResourceAsStream("res/test2.png"));
			int[] pix = lena.getRGB(0, 0, lena.getWidth(), lena.getHeight(), new int[lena.getWidth()*lena.getHeight()], 0, lena.getWidth());
			int[] lumlen = ColorConversions.getFastLuminance(pix, new int[lena.getWidth()*lena.getHeight()]);
			
			double[] ledou = Image.getScalarLuminance(lumlen, new double[lena.getWidth()*lena.getHeight()]);
			double[] secondgauss = Image.conv(ledou, lena.getWidth(), lena.getHeight(), Image.haargausxx, 9, 9, new double[lena.getWidth()*lena.getHeight()]);
			
			lumlen = Tester01.prepare(secondgauss, lumlen);
			
			pix = Tester01.fuseRGB(lumlen, lumlen, lumlen, pix);
			lena.setRGB(0, 0, lena.getWidth(), lena.getHeight(), pix, 0, lena.getWidth());
			
			JFrame ff = new JFrame();
			ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			ff.add(new JComponent() {
				{setPreferredSize(new Dimension(w*2+lena.getWidth(),lena.getHeight()));}
				@Override
				public void paint(Graphics arg0) {
					arg0.drawImage(show, 0, 0, this);
					arg0.drawImage(show2, w, 0, this);
					arg0.drawImage(lena, 2*w, 0, this);
					super.paint(arg0);
				}
			});
			
			ff.pack();
			ff.setVisible(true);
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
